<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Pages;
use Illuminate\Http\Request;

class PageController extends Controller {

    public function index(){
        $lists = Pages::orderBy('created_at', 'desc')
            ->with(['cate' => function ($query) {
                $query->select(['id','name'])->get();
            }])
            ->paginate(10);
        return $this->writeJson(0,$lists);
    }


    //详情
    public function info(Request $request){
        $id = (string)$request->get('id','0');
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        $info = Pages::findOrFail($id);
        return $this->writeJson(0,$info);
    }



    //更新
    public function update(Request $request)
    {
        $data = $request->all();
        if($data['id']){
            $info = Pages::findOrFail($data['id']);
            $info->update($data);
            return $this->writeJson(0,null,'修改单页信息成功');
        }else{
            unset($data['id']);
            Pages::create($data);
            return $this->writeJson(0,null,'新增单页信息成功');
        }
    }

    //删除
    public function destroy(Request $request){
        $id = $request->get('id');
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        if(is_array($id)){
            Pages::whereIn('id',$id)->delete();
        }else{
            Pages::destroy($id);
        }
        return $this->writeJson(0);
    }

}
